package recommend;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.stream.FileImageInputStream;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import aliyun.AliYunUpload;
import cn.imnaer.service.persistence.model.City;
import cn.imnaer.service.persistence.model.Recommend;
import cn.imnaer.service.rpc.location.CityDaoImpl;
import cn.imnaer.service.rpc.recommend.dao.RecommendDaoImpl;
import cn.imnaer.service.rpc.scenic.dao.ScenicDaoImpl;


public class DocxParser {
	
	public static void main(String[] args) throws Exception {
		BSONObject matcherd=new BasicBSONObject();
		RecommendDaoImpl.getInstance().delete(matcherd);
	    String path ="/Volumes/我的文件/城市";
        File file = new File(path);
        if(file.isDirectory()){
       File[] files = file.listFiles();  
       for (File singleFile : files) {
    	   String fileName=singleFile.getName();
    	  if(fileName.contains("docx")){
    	   FileInputStream fis = new FileInputStream(singleFile);
    	    XWPFDocument document = new XWPFDocument(fis);
        List<XWPFPictureData> picList = document.getAllPictures();
        for (XWPFPictureData pic : picList) {
            byte[] bytev = pic.getData();
           String fileExtension=    pic.suggestFileExtension();
           String contentType="image/"+fileExtension;
           ByteArrayInputStream input = new ByteArrayInputStream(bytev);             
           String key=String.valueOf(System.currentTimeMillis())+"."+fileExtension;
		   AliYunUpload.uploadFile(key, input, bytev.length, contentType); 
		   input.close();
	//插入数据库	   
		String cityName = fileName.split("\\.")[0];
		   BSONObject matcher=new BasicBSONObject();
		   matcher.put("cityName",  cityName);
	City city=	CityDaoImpl.getInstance().findOne(matcher);
	if(city!=null)	 {  
		Recommend  recommend=new Recommend();
		recommend.setImage(key);
		recommend.setName(cityName);
		recommend.setRecommendType("CITY");
		recommend.setRecommendId(city.getCityCode());
		RecommendDaoImpl.getInstance().insert(recommend, false);
	}  
        }
        fis.close();
    	}
        }
	}
       }
        	
        }
        